package com.aj.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;

public class MobileUtil {
	private static final String OS_NAME = "android";

	public static String getMacAddress(Context mContext) {
		String result = "";
		WifiManager wifiManager = (WifiManager) mContext
				.getSystemService(Context.WIFI_SERVICE);
		WifiInfo wifiInfo = wifiManager.getConnectionInfo();
		result = wifiInfo.getMacAddress();
		return result;
	}

	public static String getImei(Context mContext) {
		TelephonyManager mTm = (TelephonyManager) mContext
				.getSystemService(Context.TELEPHONY_SERVICE);
		String imei = mTm.getDeviceId();
		return imei;
	}

	public static String getOs() {
		String os = OS_NAME + " " + android.os.Build.VERSION.RELEASE;
		return os;
	}

	public static String getMobile() {
		String mobile = android.os.Build.MODEL;
		return mobile;
	}

	public static int getVersionCode(Context mContext) {
		int versionCode = Integer.MAX_VALUE;
		try {
			if (mContext != null) {
				PackageManager pm = mContext.getPackageManager();
				PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(),
						PackageManager.GET_CONFIGURATIONS);
				versionCode = pi.versionCode;
			}
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return versionCode;
	}

	public static String getVersionName(Context mContext) {
		String versionName = "";
		try {
			PackageManager pm = mContext.getPackageManager();
			PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(),
					PackageManager.GET_CONFIGURATIONS);
			versionName = pi.versionName;
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return versionName;
	}

	// 是否为中国移动手机号码
	public static boolean isCMCCPhoneNumber(String phone) {
		String regex = "^((13[4-9])|(147)|(15[0-2,7-9])|(18[2-4,7-8]))\\d{8}$";
		Pattern p = Pattern.compile(regex);
		Matcher matcher = p.matcher(phone);
		if (matcher.find()) {
			return true;
		}
		return false;
	}

	public static boolean isWifiConnect(Context context) {
		ConnectivityManager connectMgr = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = connectMgr.getActiveNetworkInfo();
		if (info != null && info.getType() == ConnectivityManager.TYPE_WIFI) {
			return true;
		} else {
			return false;
		}
	}

}
